﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Enterprise;
using ElectronicShoppingMall.Service.Models.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElectronicShoppingMall.Service.Services.Enterprise
{
    public class GoodsService : IDisposable
    {
        private ElectronicShoppingMallContext data;
        enum GoodsState
        {
            OnShelves = 0,//上架
            OffTheShelf = 1//下架
        };
        public void Dispose()
        {
            throw new NotImplementedException();
        }
        public GoodsService()
        {
            data = new ElectronicShoppingMallContext();
        }
        /// <summary>
        /// 添加商品图片
        /// </summary>
        /// <param name="goodsInfo"></param>
        /// <param name="images"></param>
        public void AddGoodsImages(int goodsId, List<string> images)
        {
            var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsId);
            foreach (var img in images)
            {
                Image goodsImg = new Image()
                {
                    GoodsId = goods.GoodsId,
                    ImagePath = "http://localhost/goodsImg/" + img
                };
                data.Images.Add(goodsImg);
                data.SaveChanges();
            }
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="goodsInfo"></param>
        public String AddGoods(GoodsInfoModel goodsInfo)
        {
            GoodsInfo goods = new GoodsInfo
            {
                AddTime = DateTime.Now,
                Category = goodsInfo.Category,
                Describe = goodsInfo.Describe,
                Details = goodsInfo.Details,
                GoodsName = goodsInfo.GoodsName,
                Price = Double.Parse(goodsInfo.Price),
                Stock = Convert.ToInt32(goodsInfo.Stock),
                State = 0,
                IsDelete = false
            };
            data.GoodsInfoes.Add(goods);
            data.SaveChanges();
            return goods.GoodsId.ToString();
        }


        /// <summary>
        /// 获取所有的商品信息
        /// </summary>
        public List<GoodsInfoModel> GetAllGoodsInfo()
        {
            List<GoodsInfoModel> result = new List<GoodsInfoModel>();
            var goods = data.GoodsInfoes;
            foreach (var goodsInfo in goods)
            {
                GoodsInfoModel temp = new GoodsInfoModel()
                {
                    GoodsId = goodsInfo.GoodsId,
                    Describe = goodsInfo.Describe,
                    Category = goodsInfo.Category,
                    Details = goodsInfo.Details,
                    GoodsName = goodsInfo.GoodsName,
                    Price = goodsInfo.Price.ToString(),
                    Stock = goodsInfo.Stock.ToString(),
                    State = goodsInfo.State,
                    AddTime = goodsInfo.AddTime.ToShortDateString()

                };
                result.Add(temp);
            }
            return result;
        }
        /// <summary>
        /// 修改商品状态（上下架）
        /// </summary>
        /// <param name="v"></param>
        public void ChangeGoodsState(int goodsId)
        {
            var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsId);
            if (goods.State == 0)
            {
                goods.State = 1;
            }
            else
            {
                goods.State = 0;
            }
            data.SaveChanges();
        }
        /// <summary>
        /// 退货入库修改库存
        /// </summary>
        /// <param name="goodsId"></param>
        /// <param name="stock"></param>
        public void ReturnGoods(int goodsId, int stock)
        {
            var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsId);
            goods.Stock += stock;
            data.SaveChanges();
        }
        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="goodsInfo"></param>
        public void ModificationOfCommodityInformation(GoodsInfoModel goodsInfo)
        {
            var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsInfo.GoodsId);
            goods.Details = goodsInfo.Details;
            goods.Describe = goodsInfo.Describe;
            goods.GoodsName = goodsInfo.GoodsName;
            goods.Price = Double.Parse(goodsInfo.Price);
            goods.Stock = Convert.ToInt32(goodsInfo.Stock);
            goods.Category = goodsInfo.Category;
            data.SaveChanges();
        }
        /// <summary>
        /// 修改新品推荐
        /// </summary>
        /// <param name="newProductId"></param>
        /// <param name="goodsId"></param>
        /// <param name="images"></param>
        public void ChangeNewProductRecommendation(int newProductId, int goodsId, List<string> images)
        {
            var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsId);
            foreach (var img in images)
            {
                Image goodsImg = new Image()
                {
                    GoodsId = goods.GoodsId,
                    ImagePath = "http://localhost/newProduct/" + img
                };
                data.Images.Add(goodsImg);
                data.SaveChanges();
                if (newProductId == -1)
                {
                    NewProduct product = new NewProduct
                    {
                        Url = "http://localhost:60861/#/CommodityDetailsPage/"+goods.GoodsId,
                        ImageId = goodsImg.ImageId,
                    };
                    data.NewProducts.Add(product);
                    data.SaveChanges();
                }
                else
                {
                    var product = data.NewProducts.SingleOrDefault(n => n.Id == newProductId);
                    var oldImage = data.Images.SingleOrDefault(i => i.ImageId == product.ImageId);
                    goods.Images.Remove(oldImage);
                    data.Images.Remove(oldImage);
                    product.ImageId = goodsImg.ImageId;
                    product.Url = "http://localhost:60861/#/CommodityDetailsPage/"+goods.GoodsId;
                    data.SaveChanges();
                }
            }
        }
        /// <summary>
        /// 获取新品推荐
        /// </summary>
        /// <returns></returns>
        public List<NewProductModel> GetNewProduct()
        {
            List<NewProductModel> result = new List<NewProductModel>();
           var products =  data.NewProducts.ToList();
            foreach(var product in products)
            {
                var img = data.Images.SingleOrDefault(i => i.ImageId == product.ImageId);
                NewProductModel temp = new NewProductModel
                {
                    Id=product.Id,
                    Image = img.ImagePath,
                    Url = product.Url
                };
                result.Add(temp);
            }
            return result;
        }
    }
}
